/* @Author      guang
 * @description 登录拦截放行时，设置虚拟角色
 */
import { defineStore } from 'pinia';
import { postAgentAuthLogout } from '@/api/index';
import router from '@/router';
// 仓库数据
// import { useStorePermission } from '@/store';
export const useStoreUser = defineStore('user', {
  state: () => {
    return {
      dlsToken: '',
      dlsUserInfo: {
        typeList: [],
        roles: [],
      },
      rsaKey: '',
      activateUser: '',
    };
  },
  actions: {
    async userLogin(params) {
      const response = await login(params);
      const accessToken = response?.data?.dlsToken;
      const activateUserInfo = response?.data?.activateUser;
      this.activateUser = activateUserInfo;
      this.getUserInfo();
      if (accessToken) {
        this.dlsToken = accessToken;
        return Promise.resolve(true);
      }
      return Promise.reject(false);
    },
    getUserInfo() {
      return new Promise(resolve => {
        userInfo().then(result => {
          this.dlsUserInfo = result.data || null;
          resolve();
        });
      });
    },
    setUserInfo(params) {
      this.dlsUserInfo = Object.assign(this.dlsUserInfo, params || {});
    },
    logout() {
      return new Promise(resolve => {
        postAgentAuthLogout()
          .then(() => {
            this.dlsToken = '';
            this.dlsUserInfo = {}
            router.replace('/login');
            resolve();
          })
          .catch(() => {
            // resolve()
          });
      });
    },
  },
  persist: {
    key: 'dls-store-user',
    storage: window.localStorage,
    paths: ['dlsUserInfo', 'dlsToken', 'rsaKey', 'activateUser'],
  },
});
